﻿using MPic.Business.DAL;
using MPic.Business.Entity;
using MPic.Business.Enums;
using MPic.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MPic.Business.BLL
{
    public class UsersBLL
    {
        private UsersDAL userDal = new UsersDAL(DbConnectionEnum.MPic);
        private static object locker = new object();
        private static UsersBLL _instance;
        public static UsersBLL Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (locker)
                    {
                        if (_instance == null)
                        {
                            _instance = new UsersBLL();
                        }
                    }
                }
                return _instance;
            }
        }

        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Register(string email, string password)
        {
            var user = new UsersEnitty
            {
                UserName = email.Trim(),
                Email = email.Trim(),
                Password = password,
                LoginTimes = 0,
                LoginIp = IPHelper.getIPAddr(),
                LastLoginDate = null,
                LastLoginIp = IPHelper.getIPAddr(),
                UploadTimes = 0,
                LastUploadDate = null,
                RegDate = DateTime.Now,
                Level = 1,
                ModifyDate = DateTime.Now,
                Remark = "",
                IsEnable = true,
                DefaultAlbum = 1,
                DefaultUploadType = 1
            };
            var uid = userDal.Insert(user).Value;
            if (uid > 0)
            {
                var albumId = UserAlbumBLL.Instance.Insert(uid, "默认相册", true);
                user = userDal.Get(uid);
                user.DefaultAlbum = albumId;
                userDal.Update(user);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据邮箱获取用户信息
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public UsersEnitty GetUser(string email)
        {
            return userDal.Get(new { Email = email }, "UserId ASC");
        }

        /// <summary>
        /// 登录获取用户信息
        /// </summary>
        /// <param name="email"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public UsersEnitty GetUser(string email, string password)
        {
            return userDal.Get(new { Email = email, Password = password }, "UserId ASC");
        }

        /// <summary>
        /// 校验是否存在该邮箱的用户
        /// </summary>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool IsExists(string email)
        {
            return userDal.RecordCount(new { Email = email }) > 0;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="defaultUploadType"></param>
        /// <returns></returns>
        public bool Update(int userId, int? defaultUploadType, int? defaultAlbum)
        {
            var user = userDal.Get(userId);
            if (defaultUploadType.HasValue)
            {
                user.DefaultUploadType = defaultUploadType.Value;
            }
            if (defaultAlbum.HasValue)
            {
                user.DefaultAlbum = defaultAlbum.Value;
            }
            return userDal.Update(user) > 0;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public bool Update(int userId, string password)
        {
            var user = userDal.Get(userId);
            user.Password = password;
            return userDal.Update(user) > 0;
        }

        /// <summary>
        /// 更新用户上传信息
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Update(int userId)
        {
            var user = userDal.Get(userId);
            user.UploadTimes += 1;
            user.LastUploadDate = DateTime.Now;
            return userDal.Update(user) > 0;
        }
    }
}
